class Solution {
public:
    int maxValue(vector<vector<int>>& events, int k) {
        int n = events.size();
        vector<vector<int> > dp(n+1,vector<int>(k+1));
        sort(events.begin(),events.end(),[](auto& a,auto& b){return a[1] < b[1];});
        int Max = 0;
        for(int i = 0;i < n;i++){
             int l = lower_bound(events.begin(),events.end(),events[i][0],[](auto& c,int target){return c[1] < target;})-events.begin();

            // int l = 0;
            // int r = i;
            // while(l <= r){
            //     int mid = l + (r - l) / 2;
            //     if(events[mid][1] < events[i][0]){
            //         l = mid+1;
            //     }else{
            //         r = mid-1;
            //     }
            // }
            
           for(int j = k;j >= 1;j--){
                dp[i+1][j] = max(dp[i][j],dp[l][j-1]+events[i][2]);
           }
        }
        return dp[n][k];
    }
};
